package net.sareweb.acab.actions.suggestion;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;

import net.sareweb.acab.entity.Commentonsuggestion;
import net.sareweb.acab.entity.Suggestion;
import net.sareweb.acab.entity.Suggestioninfo;
import net.sareweb.acab.entity.User;
import net.sareweb.acab.entity.manager.CommentOnSuggestionManager;
import net.sareweb.acab.entity.manager.SuggestionManager;
import net.sareweb.acab.entity.manager.UserManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;

@Name("suggestionBean")
@Scope(ScopeType.CONVERSATION)
public class SuggestionBean {
	
	@Logger
	private Log log;

	@In 
	Credentials credentials;
	
	@In(create=true)
	SuggestionManager suggestionManager;
	@In(create=true)
	CommentOnSuggestionManager commentOnSuggestionManager;
	@In(create=true)
	UserManager userManager;
	@In
	protected EntityManager entityManager;
	
	Suggestioninfo suggestioninfo;
	String sentMessage;


	
	private Integer idSuggestion; 
	
	
	public String  loadSuggestionAction(){
		log.debug("loadSuggestionAction, idSuggestion value = {0}", idSuggestion);
		if(idSuggestion==null || idSuggestion.intValue()==0) return "error";
		Suggestion suggestion=(Suggestion) suggestionManager.find(idSuggestion);
		suggestioninfo = suggestion.getSuggestioninfo();
		return "";
	}
	
	public List<Commentonsuggestion> getCommentsOnSuggestion(){
		log.debug("Call to getCommentsOnSuggestion", "");
		//entityManager.refresh(suggestioninfo);
		//return suggestioninfo==null? null: new ArrayList<Commentonsuggestion>(suggestioninfo.getCommentonsuggestions());
		return commentOnSuggestionManager.getSuggestionComments(suggestioninfo.getIdSuggestionInfo());
	}

	
	
	public String sendMessageAction(){
		log.debug("sentMessage : {0}", sentMessage);
		if(sentMessage==null || sentMessage.equals(""))return "";
		Commentonsuggestion commOnSugg = new Commentonsuggestion();
		User user = userManager.findByLoginName(credentials.getUsername());
		commOnSugg.setUser(user);
		commOnSugg.setSuggestioninfo(suggestioninfo);
		commOnSugg.setMsg(sentMessage);
		commOnSugg.setCreateDate(new Date());
		log.debug("create date mills: {0}", commOnSugg.getCreateDate().getTime());
		commentOnSuggestionManager.save(commOnSugg);
		commentOnSuggestionManager.flush();
		sentMessage="";
		return "";
		
	}

	
	





	public Integer getIdSuggestion() {
		return idSuggestion;
	}


	public void setIdSuggestion(Integer idSuggestion) {
		this.idSuggestion = idSuggestion;
	}




	public Suggestioninfo getSuggestioninfo() {
		return suggestioninfo;
	}


	public void setSuggestioninfo(Suggestioninfo suggestioninfo) {
		this.suggestioninfo = suggestioninfo;
	}

	public String getSentMessage() {
		return sentMessage;
	}

	public void setSentMessage(String sentMessage) {
		this.sentMessage = sentMessage;
	}
	
	
	
	
	
	
	
}
